package kr.hkit.powerjava.chapter14.windowbuilder;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class CalculatorEx extends JFrame implements ActionListener {

	private JPanel contentPane;
	private CalPanel panelGet;
	private JPanel panel_1;
	private JButton btnClear;
	private JButton btnCal;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					CalculatorEx frame = new CalculatorEx();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public CalculatorEx() {
		initialize();
	}
	private void initialize() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		panelGet = new CalPanel();
		contentPane.add(panelGet, BorderLayout.CENTER);
		
		panel_1 = new JPanel();
		contentPane.add(panel_1, BorderLayout.SOUTH);
		
		btnCal = new JButton("계산");
		btnCal.addActionListener(this);
		panel_1.add(btnCal);
		
		btnClear = new JButton("Clear");
		btnClear.addActionListener(this);
		panel_1.add(btnClear);
		
		pack();
	}

	public void actionPerformed(ActionEvent e) {
		if(e.getActionCommand().equals("계산")){
			
			//			System.out.printf("%s * %s", panel.getTfLeft(), panel.getTfRight());
			try{
			int left = Integer.parseInt(panelGet.getTfLeft().trim());
			int right = Integer.parseInt(panelGet.getTfRight().trim());
			panelGet.setTfResult(left*right+"");
			}catch(NumberFormatException e1){
				JOptionPane.showMessageDialog(this, "숫자만 입력하시오");
			}
			
		}else{
			panelGet.setTfLeft("");
			panelGet.setTfRight("");
			panelGet.setTfResult("");
//			panel.repaint();
		}
	}
}
